<template>
  <!-- 面板 -->
  <div class="pad">
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="标签名称" style="padding: 0 30px 0 10px">
          <el-input v-model="pagege.tagName"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="state" placeholder="请选择">
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetForm()">清除</el-button>
          <el-button type="primary" @click="Search">搜索</el-button>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="success" icon="el-icon-edit" @click="onSubmit"
            >新增目录</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 数据显示条数 -->
      <el-alert title="数据一共有12条" type="info" :closable="false" show-icon>
      </el-alert>
      <!-- 表格显示 -->
      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="tagName" label="标签名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column label="创建日期">
          <template v-slot="state">
            {{ dateHandler(state.row.addDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量"> </el-table-column>
        <el-table-column prop="state" label="状态">
          <template v-slot="state">
            {{ state.row.state === 0 ? '已启用' : '已禁用' }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作">
          <template v-slot="state">
            <el-button @click="ford(state.row)" type="text" size="small">
              {{ state.row.state === 1 ? '禁用' : '启用' }}
            </el-button>
            <el-button
              type="text"
              size="small"
              :disabled="state.row.state === 1"
              @click="changelist(state)"
            >
              修改</el-button
            >
            <el-button
              :disabled="state.row.state === 1"
              type="text"
              size="small"
              @click="deleteHandler(state.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="float: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="pagege.pagesize"
        background
        layout="prev, pager, next, sizes,jumper"
        :total="totals"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      v-if="dialogFormVisible"
      :title="checkvalue ? '新增标签' : '修改标签'"
      :visible.sync="dialogFormVisible"
      width="30%"
    >
      <pyx
        ref="datapyx"
        :listHandler="listHandler"
        :checkvalue="checkvalue"
        @success="success"
      />
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, tagSimple, changeState } from '@/api/hmmm/tags.js'
import pyx from '../components/tags-add.vue'
const moment = require('moment')
export default {
  components: {
    pyx
  },
  data () {
    return {
      pagege: {
        page: 1,
        pagesize: 10,
        tagName: ''
      },
      state: '',
      checkvalue: false,
      list: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      adlist: {
        subjectID: '',
        tagName: ''
      },
      ID: 1,
      totals: 0
    }
  },
  created () {
    this.listHandler()
    this.loadSimple()
  },
  methods: {
    // 清楚搜索框

    async loadSimple () {
      const res = await tagSimple()
      console.log(res.data, 99)
      if (this.dialogFormVisible) {
        this.$refs.datapyx.listform = { ...res.data }
      }
    },
    resetForm () {
      this.pagege = {
        page: 1,
        pagesize: 10,
        tagName: ''
      }
      this.listHandler()
    },

    handleClick (row) {
      console.log(row)
    },
    // 按钮的禁用和启用
    async ford (row) {
      if (row.state === 1) {
        row.state = 0
      } else {
        row.state = 1
      }
      await changeState(row)
    },
    // 获取页面的数据表格
    async listHandler () {
      const res = await list(this.pagege)
      if (res.status === 200) {
        this.list = res.data.items
        this.totals = res.data.counts
        console.log(res)
      }
      console.log(res)
    },
    // 转换日期格式
    dateHandler (date) {
      let createTime = date
      createTime = moment(createTime).format('YYYY-MM-DD HH:mm:ss')
      return createTime
    },
    // 分页控制
    handleSizeChange (val) {
      this.pagege.pagesize = val

      this.listHandler()
    },
    handleCurrentChange (val) {
      this.pagege.page = val

      this.listHandler()
    },

    // 添加页面
    onSubmit () {
      this.dialogFormVisible = true
      this.checkvalue = true
    },
    // 修改页面
    changelist (state) {
      this.checkvalue = false
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs.datapyx.listform = JSON.parse(JSON.stringify(state.row))
      })
    },
    deleteHandler (row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          await remove(row)
          this.listHandler()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    success () {
      this.dialogFormVisible = false
      this.listHandler()
    },
    async Search () {
      this.pagege.state = this.state
      this.listHandler()
    }
  }
}
</script>

<style scoped lang="scss">
.pad {
  margin: 10px 10px;
}
</style>
